<template>
	<view class="content">
		<scroll-view scroll-x class="sub_board_scroll app_content_color" v-if="subBoardList.length > 0">
		<view class="sub_board_content app_content_color">
			
			<view class="sub_board_cell" v-for="(item,index) in subBoardList" @tap="subBoardTap(item)">
				<view>{{item.board_name}}</view>
			</view>
			
			</view>
		</scroll-view>
		<post-list-cell class="forum_post_cell_content" v-for="(item,index) in items" :key="index" :item="item">
		</post-list-cell>
	</view>
</template>

<script>
	import http from "../../utils/http.js"
	import util from "../../utils/util.js"
	export default {
		data() {
			return {
				sortby: "",
				boardId:"",
				page:1,
				items:[],
				subBoardList:[]
			}
		},
		onLoad(option){
			uni.setNavigationBarTitle({
				title:option.title
			})
			this.sortby = option.sortby?option.sortby:"all";
			this.boardId = option.boardId?option.boardId:"all";
			this.loadData();
			this.loadSubBoardList();
		},
		onPullDownRefresh(){
			this.loadData();
			this.loadSubBoardList();
		},
		onReachBottom(){
			this.loadMoreData();
		},
		methods: {
			loadSubBoardList(){
				if(this.boardId != "all"){
					let that = this;
					http.request("forum/forumlist",{fid : this.boardId},{
						success:function(data){
							if(data.list.length > 0){
								that.subBoardList = data.list[0].board_list
							}
						},
						fail: function(msg) {
					
						}
					})
				}
			},
			loadData(){
				var that = this;
				http.request("forum/topiclist",{pageSize : 20,page: 1,boardId:this.boardId,sortby:this.sortby},{
					success:function(data){
						uni.stopPullDownRefresh();
						that.items = data.list.map(function(item){
							item.showTime = util.dateStringFromTimestamp(item.last_reply_date);
							return item;
						})
						that.page = 2;
					},
					fail: function(msg) {
						uni.stopPullDownRefresh();
						uni.showToast({
							title:msg,
							icon:"none"
						})
					}
				})
			},
			loadMoreData(){
				var that = this;
				http.request("forum/topiclist",{pageSize : 20,page: this.page,boardId:this.boardId,sortby:this.sortby},{
					success:function(data){
						var items = that.items.concat( data.list.map(function(item){
							item.showTime = util.dateStringFromTimestamp(item.last_reply_date);
							return item;
						}))
						that.items = items;
						that.page ++;
					},
					fail: function(msg) {
						uni.showToast({
							title:msg,
							icon:"none"
						})
					}
				})
			},
			subBoardTap(item){
				uni.navigateTo({
					url:"../forum-post-list/forum-post-list?title="+item.board_name+"&boardId="+item.board_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		width: 100%;
		flex-direction: column;
	}
	
	.sub_board_scroll{
		display: flex;
		flex-direction: row;
		width: 100vw;
	}
	
	.sub_board_content{
		display: flex;
		flex-direction: row;
		padding-top: 20upx;
		padding-bottom: 20upx;
		padding-left: 30upx;
		padding-right: 30upx;
	}
	
	.sub_board_cell{
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1px solid $uni-global-green-color;
		color: $uni-global-green-color;
		font-size: 30upx;
		height: 60upx;
		border-radius: 6upx;
		margin-right: 30upx;
		padding-left: 30upx;
		padding-right: 30upx;
		white-space: nowrap;
	}

</style>
